package com.zr.d_c.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.d_c.dao.FuncDao;
import com.zr.d_c.model.Func;
import com.zr.d_c.util.JDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class FuncDaoImpl implements FuncDao {

	public JSONArray selectFuncByParentIdAndFids(int parentid, String ids) {
		// TODO Auto-generated method stub
		String[] id = ids.split(",");
		JSONArray funcs = new JSONArray();
		
		Connection con = null;
		PreparedStatement pst = null;
		StringBuffer sql = new StringBuffer("select * from d_cfunc where fparentid = ?");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, parentid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				JSONObject func = new JSONObject();
				boolean flag = false;
				for (int i = 0; i < id.length; i++) {
					if(rs.getInt("fid") == Integer.parseInt(id[i])){
						flag = true;
						break;
					}
				}
				if(flag){
					func.put("id", rs.getInt("fid"));
					func.put("text", rs.getString("fname"));
					if(rs.getString("fstate").equals("open")){
						func.put("iconCls", "icon-filter");
					}else{
						func.put("iconCls", "icon-sum");
					}
					func.put("state", rs.getString("fstate"));
					JSONObject attr = new JSONObject();
					attr.put("fpath", rs.getString("fpath"));
					func.put("attributes", attr);
					funcs.add(func);
				}	
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return funcs;
	}

	public JSONArray selectAllFuncByParentId(int parentid, String ids) {
		// TODO Auto-generated method stub
		String[] id = ids.split(",");
		JSONArray funcs = new JSONArray();
		
		Connection con = null;
		PreparedStatement pst = null;
		StringBuffer sql = new StringBuffer("select * from d_cfunc where fparentid = ?");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, parentid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				JSONObject func = new JSONObject();
				boolean flag = false;
				for (int i = 0; i < id.length; i++) {
					if(rs.getInt("fid") == Integer.parseInt(id[i])){
						flag = true;
						break;
					}
				}
				if(flag){
					func.put("id", rs.getInt("fid"));
					func.put("text", rs.getString("fname"));
					if(rs.getString("fstate").equals("open")){
						func.put("iconCls", "icon-filter");	
					}else{
						func.put("iconCls", "icon-sum");
					}
					func.put("checked", true);
					func.put("state", rs.getString("fstate"));
					JSONObject attr = new JSONObject();
					attr.put("fpath", rs.getString("fpath"));
					func.put("attributes", attr);
					funcs.add(func);
				}else{
					func.put("id", rs.getInt("fid"));
					func.put("text", rs.getString("fname"));
					if(rs.getString("fstate").equals("open")){
						func.put("iconCls", "icon-filter");
					}else{
						func.put("iconCls", "icon-sum");
					}
					func.put("state", rs.getString("fstate"));
					JSONObject attr = new JSONObject();
					attr.put("fpath", rs.getString("fpath"));
					func.put("attributes", attr);
					funcs.add(func);
				}	
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return funcs;
	}

	public JSONArray selectAllFuncByParentId(int parentid) {
		// TODO Auto-generated method stub
		JSONArray funcs = new JSONArray();
		
		Connection con = null;
		PreparedStatement pst = null;
		StringBuffer sql = new StringBuffer("select * from d_cfunc where fparentid = ?");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, parentid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				JSONObject func = new JSONObject();
				func.put("id", rs.getInt("fid"));
				func.put("text", rs.getString("fname"));
				if(rs.getString("fstate").equals("open")){
					func.put("iconCls", "icon-filter");
				}else{
					func.put("iconCls", "icon-sum");
				}
				func.put("state", rs.getString("fstate"));
				JSONObject attr = new JSONObject();
				attr.put("fpath", rs.getString("fpath"));
				func.put("attributes", attr);
				funcs.add(func);
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return funcs;
	}

	public Func selectFuncByFname(String fname) {
		// TODO Auto-generated method stub
		Func f = new Func();
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("select * from d_cfunc where fname = ?");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, fname);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				f.setFid(rs.getInt("fid"));
				f.setFname(rs.getString("fname"));
				f.setFparentid(rs.getInt("fparentid"));
				f.setFpath(rs.getString("fpath"));
				f.setFstate(rs.getString("fstate"));
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return f;
	}

	public int deleteFuncByFname(String fname) {
		// TODO Auto-generated method stub
		int flag = 0;
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("delete from d_cfunc where fname = ?");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, fname);
			int i = pst.executeUpdate();
			if(i > 0){
				flag = 1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return flag;
	}

	public int insertFunc(Func f) {
		// TODO Auto-generated method stub
		int flag = 0;
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("insert into d_cfunc (fname,fpath,fparentid,fstate) values(?,?,?,?)");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, f.getFname());
			pst.setString(2, f.getFpath());
			pst.setInt(3, f.getFparentid());
			pst.setString(4, f.getFstate());
			int i = pst.executeUpdate();
			if(i > 0){
				flag = 1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return flag;
	}

	public List<Func> selectAllFunc() {
		// TODO Auto-generated method stub
		List<Func> funcs = new ArrayList<Func>();
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("select * from d_cfunc");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Func f = new Func();
				f.setFid(rs.getInt("fid"));
				f.setFname(rs.getString("fname"));
				f.setFparentid(rs.getInt("fparentid"));
				f.setFpath(rs.getString("fpath"));
				f.setFstate(rs.getString("fstate"));
				funcs.add(f);
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return funcs;
	}

	@Override
	public List<Func> selectFirstFunc() {
		// TODO Auto-generated method stub
		List<Func> funcs = new ArrayList<Func>();
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("select * from d_cfunc where fparentid = 1");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				if(rs.getString("fstate").equals("closed")){
					Func f = new Func();
					f.setFid(rs.getInt("fid"));
					f.setFname(rs.getString("fname"));
					f.setFparentid(rs.getInt("fparentid"));
					f.setFpath(rs.getString("fpath"));
					f.setFstate(rs.getString("fstate"));
					funcs.add(f);
				}
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return funcs;
	}

	

}
